package com.duolaibei.business.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.duolaibei.business.R;
import com.duolaibei.business.R.id;
import com.duolaibei.business.base.BaseActivity;
import com.duolaibei.business.config.Preferences;
import com.duolaibei.business.utils.CommonUtil;
import com.duolaibei.business.utils.PreferenceUtil;
import com.duolaibei.business.utils.inject.Inject;
import com.duolaibei.business.view.dialog.BluePrintNumDialog;
import com.duolaibei.business.view.dialog.BluePrintNumDialog.Listener;

/**
 * Created by xlm on 2017/11/11 0011
 */

public class BlueToothStateActivity extends BaseActivity {

    @Inject(id.back)
    private RelativeLayout mLayoutBack;
    @Inject(id.title)
    private TextView mViewTitle;

    @Inject(id.bluetooth_state_press)
    private RelativeLayout mLayoutBluetoothState;
    @Inject(id.bluetooth_print_press)
    private RelativeLayout mLayoutBluetoothPrint;
    @Inject(id.bluetooth_state)
    private TextView mTextViewState;
    @Inject(id.bluetooth_print_num)
    private TextView mTextViewNum;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_bluetooth_state);
    }

    @Override
    protected void initViews() {
        mViewTitle.setText("蓝牙打印");
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        setPrintNum();
        setBluestate();
    }

    /**
     * 设置打印张数
     */
    private void setPrintNum() {
        String num = String.valueOf(PreferenceUtil.getParam(Preferences.PRINTNUM, ""));
        if (!CommonUtil.isEmpty(num)) {
            mTextViewNum.setText(num + "张");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        setPrintNum();
        setBluestate();
    }

    /**
     * 设置蓝牙的状态
     */
    private void setBluestate() {
        //蓝牙为连接状态
        if ("true".equals(String.valueOf(PreferenceUtil.getParam(Preferences.ISCONNECTED, "")))) {
            mTextViewState.setText("已连接");
        } else {
            mTextViewState.setText("未连接");
        }
    }

    @Override
    protected void onSetListener() {
        mLayoutBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //蓝牙状态
        mLayoutBluetoothState.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(BlueToothStateActivity.this.getThis(), BluetoothPrintActivity.class);
                startActivity(intent);
            }
        });
        //打印张数
        mLayoutBluetoothPrint.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showPrintNumDialog();
            }
        });
    }

    private BluePrintNumDialog mBluePrintNumDialog;

    /**
     * 选择小票打印张数
     */
    private void showPrintNumDialog() {
        if (this.mBluePrintNumDialog == null) {
            this.mBluePrintNumDialog = new BluePrintNumDialog(this.getThis());
        }
        this.mBluePrintNumDialog.setCanceledOnTouchOutside(true);
        this.mBluePrintNumDialog.setListener(new Listener() {
            @Override
            public void printNum(String num) {
                super.printNum(num);
                mTextViewNum.setText(num);
                //保存打印张数
                PreferenceUtil.setParam(Preferences.PRINTNUM, num);
            }
        });
        this.mBluePrintNumDialog.show();
    }

}
